`
https://leetcode.cn/problems/recover-binary-search-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {void} Do not return anything, modify root in-place instead.
 */
var recoverTree = function (root) {
  let swap1 = null, swap2 = null

  let lastNode = null

  const traverse = (root) => {
    if (root === null) return

    traverse(root.left)

    if (lastNode && lastNode.val > root.val) {
      // 两个错误的结点不一定在中序遍历里是紧挨着的
      // swap1 拿到第一个错误的结点之后就不用再被赋值了
      // 由 swap2 去找到下一个错误的结点即可
      if (!swap1) {
        swap1 = lastNode
      }
      swap2 = root
    }
    lastNode = root

    traverse(root.right)
  }

  traverse(root)

  if (swap1 && swap2) {
    [swap1.val, swap2.val] = [swap2.val, swap1.val]
  }

  return root
};